package com.ecube.mqtt.page.listener;

import com.ecube.mqtt.page.listener.chargelistener.BackListener;
import com.ecube.mqtt.page.listener.swapListener.SwapRateListener;
import com.ecube.mqtt.page.listener.swapListener.SwapStartListener;
import com.ecube.mqtt.page.listener.swapListener.SwapStartResultPageListener;
import com.ecube.mqtt.page.listener.swapListener.SwapTCUStartListener;
import com.ecube.mqtt.utils.PropertyUtil;
import com.ecube.mqtt.utils.CommonConstant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class SwapListener implements ActionListener {
    private JButton chargeButton;
    private String path;
    private JFrame first;

    public SwapListener(JFrame first, JButton chargeButton, String path) {//获取登录界面、按钮对象
        this.first = first;
        this.path = path;
        this.chargeButton = chargeButton;
    }


    public void actionPerformed(ActionEvent e) {
        first.dispose();
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);
        Font font = new Font("宋体",Font.PLAIN,18);

        //生成新界面
        JFrame charge = new JFrame();
        charge.setSize(500, 750);
        charge.setTitle("换电");//设置窗体标题
        charge.setDefaultCloseOperation(2);
        charge.setLocationRelativeTo(null);
        charge.setFont(font);  //宋体，正常风格，14号字体
        //实例化FlowLayout流式布局类的对象，指定对齐方式为居中对齐组件之间的间隔为10个像素
        FlowLayout fl = new FlowLayout(FlowLayout.RIGHT,30,30);
        //实例化流式布局类的对象
        charge.setLayout(fl);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel labname = new JLabel("环境：");
        labname.setFont(font);
        //将labname标签添加到窗体上
        charge.add(labname);
        String envfirst = "";
        try {
            envfirst = PropertyUtil.getProperties("env", CommonConstant.SWAP_CONFIG_PATH);
           // path + "config\\swap.properties"
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        JComboBox env = new JComboBox();
        env.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        env.setFont(font);
        env.addItem("DEV");
        env.addItem("FAT");
        env.addItem("UAT");
        env.addItem("HG");
        env.setSelectedItem(envfirst);
        //env.getSelectedItem().toString()
        charge.add(env);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel stationname = new JLabel("站ID：");
        stationname.setFont(font);
        //将labname标签添加到窗体上
        charge.add(stationname);

        String stationIdfirst = "";
        try {
            stationIdfirst = PropertyUtil.getProperties("stationId", CommonConstant.SWAP_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField stationId = new JTextField(stationIdfirst);
        stationId.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        stationId.setFont(font);
        //将textName标签添加到窗体上
        charge.add(stationId);


        //实例化JLabel标签对象，该对象显示“账号”
        JLabel vehicle = new JLabel("车牌：");
        vehicle.setFont(font);
        charge.add(vehicle);


        String vehiclefirst = "";
        try {
            vehiclefirst = PropertyUtil.getProperties("vehicle", CommonConstant.SWAP_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField vehicletext = new JTextField(vehiclefirst);
        vehicletext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        vehicletext.setFont(font);
        charge.add(vehicletext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel time = new JLabel("充电时长min：");
        time.setFont(font);
        charge.add(time);

        //实例化JTextField标签对象化
        JTextField timetext = new JTextField("200");
        timetext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        timetext.setFont(font);
        charge.add(timetext);


        //实例化JLabel标签对象，该对象显示“账号”
        JLabel startSoc = new JLabel("换下soc：");
        startSoc.setFont(font);
        charge.add(startSoc);

        //实例化JTextField标签对象化
        JTextField startSoctext = new JTextField("87.6");
        startSoctext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        startSoctext.setFont(font);
        charge.add(startSoctext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel endSoc = new JLabel("换上soc：");
        endSoc.setFont(font);
        charge.add(endSoc);

        //实例化JTextField标签对象化
        JTextField endSoctext = new JTextField("100");
        endSoctext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        endSoctext.setFont(font);
        charge.add(endSoctext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel model = new JLabel("换电模式：");
        model.setFont(font);
        charge.add(model);

        JComboBox modeltext = new JComboBox();
        modeltext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        modeltext.setFont(font);
        modeltext.addItem("云端");
        modeltext.addItem("站控");
        modeltext.addItem("手动");
        modeltext.setSelectedItem("云端");
        charge.add(modeltext);


        //实例化JButton组件
        JButton startbutton = new JButton();
        //设置按键的显示内容
        startbutton.setText("     换电记录    ");
        startbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        startbutton.setSize(dim3);
        charge.add(startbutton);

        SwapStartListener cemqstart = new SwapStartListener(charge,
                env,
                stationId,
                vehicletext,
                modeltext,
                startSoctext,
                endSoctext,
                path);
        startbutton.addActionListener(cemqstart);

        //实例化JButton组件
        JButton tcuStartbutton = new JButton();
        //设置按键的显示内容
        tcuStartbutton.setText("    充电记录     ");
        tcuStartbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        tcuStartbutton.setSize(dim3);
        charge.add(tcuStartbutton);

        SwapTCUStartListener cemqtcustart = new SwapTCUStartListener(charge,
                env,
                stationId,
                vehicletext,
                modeltext,
                timetext,
                startSoctext,
                endSoctext,
                path);
        tcuStartbutton.addActionListener(cemqtcustart);

        //实例化JButton组件
        JButton startResultButton = new JButton();
        //设置按键的显示内容
        startResultButton.setText("  启动结果模拟   ");
        startResultButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        startResultButton.setSize(dim3);
        charge.add(startResultButton);

        SwapStartResultPageListener cemqStartResult = new SwapStartResultPageListener(charge,
                env,
                stationId,
                vehicletext,
                path);
        startResultButton.addActionListener(cemqStartResult);

        //实例化JButton组件
        JButton rateButton = new JButton();
        //设置按键的显示内容
        rateButton.setText("    费率下发     ");
        rateButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        rateButton.setSize(dim3);
        charge.add(rateButton);

        SwapRateListener rateResult = new SwapRateListener(charge,
                env,
                stationId,
                path);
        rateButton.addActionListener(rateResult);

        //实例化JButton组件
        JButton chargeReturnFirstPageButton = new JButton();
        //设置按键的显示内容
        chargeReturnFirstPageButton.setText("                 返回首页                ");
        chargeReturnFirstPageButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chargeReturnFirstPageButton.setSize(dim3);
        charge.add(chargeReturnFirstPageButton);



        BackListener chargeBack = new BackListener(charge, path);
        chargeReturnFirstPageButton.addActionListener(chargeBack);

        charge.setResizable(false);
        charge.setVisible(true);
    }
}